package com.feeyo.groundservice.vehiclemonitorserver;

import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.MapGeometry;
import com.esri.core.geometry.OperatorImportFromGeoJson;
import com.esri.core.geometry.WktImportFlags;
import com.feeyo.groundservice.base.rdb.PostgresDb;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by fooevr on 2016/12/23.
 */
public class LimitAreaManager extends AbstractVerticle {
	Logger logger = LoggerFactory.getLogger(LimitAreaManager.class);
	
    public static final Map<String, List<Map<String, Object>>> limits = new ConcurrentHashMap<String, List<Map<String, Object>>>();

    @Override
    public void start(Future<Void> startFuture) throws Exception {
    	try{
	        PostgresDb.postgreSQLClient.getConnection(getConn -> {
	            if (getConn.failed()){
	                System.out.println(getConn.cause());
	                return;
	            }
	            if(getConn.succeeded()){
		            getConn.result().query("select id,name,ST_AsGeoJSON(area) as area, limit_speed, posowner,_deleted,depts,types from vehiclemonitor.limitarea as a where a.area_type=2", ar->{
		                getConn.result().close();
		                if(ar.succeeded()){
		                    limits.clear();
		                    for(int i = 0; i < ar.result().getNumRows(); i++){
		                    	//System.out.println("area->"+ar.result().getRows().get(i).getString("area"));
		                        MapGeometry area = OperatorImportFromGeoJson.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Polygon, ar.result().getRows().get(i).getString("area"), null);
		                        Object dataRs = ar.result().getRows().get(i);
		                        String owner = ar.result().getRows().get(i).getString("posowner");
		                        if (!limits.containsKey(owner)){
		                            limits.put(owner, new CopyOnWriteArrayList<>());
		                        }
		                        Map<String, Object> item = new HashMap<>();
		                        item.put("id", ar.result().getRows().get(i).getString("id"));
		                        item.put("limit_speed", ar.result().getRows().get(i).getDouble("limit_speed"));
		                        item.put("posowner", ar.result().getRows().get(i).getString("posowner"));
		                        item.put("depts", ar.result().getRows().get(i).getString("depts"));
		                        item.put("types", ar.result().getRows().get(i).getString("types"));
		                        item.put("area", area);
		                        item.put("deleted", ar.result().getRows().get(i).getBoolean("_deleted"));
		                        limits.get(owner).add(item);
		                    }
		                }
		                logger.info("load limitarea " + limits.size());
		                startFuture.complete();
		            });
	            }
	        });
    	}catch (Exception e){
    		e.printStackTrace();
    	}
    }
}
